package com.example.secondhandshop.ui.adapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.secondhandshop.R;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.ui.activity.LoginAcitivity;

import java.util.Date;
import java.util.List;

public class AllchatitemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Chatitem> chatitemList;
    public AllchatitemAdapter(List<Chatitem> chatitemList) {
        this.chatitemList = chatitemList; // 聊天信息列表
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        if(viewType==2){
            view = inflater.inflate(R.layout.message_right, parent, false);
            return new ChatItemViewHolder1(view);
        }else {
            view = inflater.inflate(R.layout.message_left, parent, false);
            return new ChatItemViewHolder2(view);
        }
    }
    public void updateChatItems(List<Chatitem> chatitemList) {
        this.chatitemList = chatitemList;
        notifyDataSetChanged(); // 更新适配器中的数据显示
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Chatitem chatitem = chatitemList.get(position); // 获取当前消息
        if(holder instanceof ChatItemViewHolder1){
            ChatItemViewHolder1 chatItemViewHolder1=(ChatItemViewHolder1) holder;
            chatItemViewHolder1.tvcontent.setText(chatitem.getContent());
            chatItemViewHolder1.tvstatus.setText(chatitem.isStatus() ? "已读" : "未读");
            long createTime = chatitem.getCreateTime();
            Date date = new Date(createTime);
            String createTimeString = date.toString();
            chatItemViewHolder1.tvcreateTime.setText(createTimeString);
        }else {
            ChatItemViewHolder2 chatItemViewHolder2=(ChatItemViewHolder2) holder;
            chatItemViewHolder2.tvcontent.setText(chatitem.getContent());
            chatItemViewHolder2.tvstatus.setText(chatitem.isStatus() ? "已读" : "未读");
            long createTime = chatitem.getCreateTime();
            Date date = new Date(createTime);
            String createTimeString = date.toString();
            chatItemViewHolder2.tvcreateTime.setText(createTimeString);
        }

    }

    @Override
    public int getItemCount() {
        return chatitemList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(chatitemList.get(position).getUserId()==LoginAcitivity.userid){
            return 1; // 返回消息类型 自己
        }else {
            return 2;//对方
        }
    }

    static class ChatItemViewHolder1 extends RecyclerView.ViewHolder {
        TextView tvcontent;
        TextView tvcreateTime;
        TextView tvstatus;
        ImageView imageView;
        public ChatItemViewHolder1(View itemView) {
            super(itemView);
            tvcontent = itemView.findViewById(R.id.textViewMessage);
            tvcreateTime = itemView.findViewById(R.id.textViewTimeSent);//先默认为发送方
            tvstatus = itemView.findViewById(R.id.textViewReadStatus);
            imageView = itemView.findViewById(R.id.imageViewAvatar); // 头像
        }

    }
    static class ChatItemViewHolder2 extends RecyclerView.ViewHolder {
        TextView tvcontent;
        TextView tvcreateTime;
        TextView tvstatus;
        ImageView imageView;
        public ChatItemViewHolder2(View itemView) {
            super(itemView);
            tvcontent = itemView.findViewById(R.id.textViewMessage);
            tvcreateTime = itemView.findViewById(R.id.textViewTimeReceived);//先默认为发送方
            tvstatus = itemView.findViewById(R.id.textViewReadStatus);
            imageView = itemView.findViewById(R.id.imageViewAvatar); // 头像
        }

    }

}